package com.ruoyi.partybuilding.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.partybuilding.common.EQueryHelper;
import com.ruoyi.partybuilding.domain.vo.ReviewStatVo;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.partybuilding.mapper.ReviewMapper;
import com.ruoyi.partybuilding.domain.Review;
import com.ruoyi.partybuilding.service.IReviewService;

import javax.annotation.Resource;

/**
 * 评议Service业务层处理
 * 
 * @author he-liangliang@outlook.com
 * @date 2024-11-27
 */
@Service
public class ReviewServiceImpl implements IReviewService 
{
    @Autowired
    private ReviewMapper reviewMapper;


    @Resource
    protected EQueryHelper queryHelper;
     final static String sql="SELECT r.*,r.id review_id,ru1.grade,ru2.*,s.user_id reviewed_user,s.user_name reviewed_user_name,s.dept_id reviewed_user_dept,s.dept_name reviewed_user_dept_name,s.avatar from pb_review_staffs s left join pb_review r on r.id=s.review_id and s.type=0 left join pb_review_user ru1 on ru1.review_id=r.id and ru1.type=2 and ru1.reviewed_user=s.user_id left join ( SELECT review_id review_id2,reviewed_user reviewed_user2,MAX(reviewed_user_dept) rdept,max(reviewed_user_dept_name) reviewed_user_dept_name,max(reviewed_user_name) reviewed_user_name2, sum(case WHEN grade=0 then 1 else 0 end) grade0, sum(case WHEN grade=1 then 1 else 0 end) grade1, sum(case WHEN grade=2 then 1 else 0 end) grade2, sum(case WHEN grade=3 then 1 else 0 end) grade3, sum(case WHEN grade=4 then 1 else 0 end) grade4, sum(case WHEN grade=5 then 1 else 0 end) grade5 from pb_review_user where type=0 GROUP BY review_id,reviewed_user ) ru2 on ru2.review_id2=r.id and ru2.reviewed_user2=s.user_id where 1=1 ";

    public R<List<ReviewStatVo>> statReview(ReviewStatVo vo) {
         var list = queryHelper.CreateItems(sql)
                 .and(" r.id=? ", vo.getReviewId())
                 .andNotNull(" s.dept_id=? ",vo.getReviewedUserDept())
                 .resultPkg().sqlQuery(ReviewStatVo.class);
         return R.ok(list);
     }

    /** 根据时间更新状态
     *
     * @param pData 要检查的对象
     * @param nowDate 时间点
     * @return 更新的对象
     */
    public List<Review> statusSetByNow(Date nowDate,Review ...pData) {

        var upList = Arrays.stream(pData)
                .filter(p -> {
                    if(p.getStatus()==null) {
                        if (nowDate.compareTo(p.getEnd()) > 0)
                            p.setStatus(1);
                        else if (nowDate.compareTo(p.getBegin()) > 0)
                            p.setStatus(0);
                        else  //if (nowDate.compareTo(p.getBegin()) < 0)
                            p.setStatus(2);
                        return true;
                    }
                    if (p.getStatus() == 1)
                        return false;
                    if (nowDate.compareTo(p.getEnd()) > 0)
                        p.setStatus(1);
                    else if (p.getStatus() == 2 && nowDate.compareTo(p.getBegin()) > 0) {
                        p.setStatus(0);
                    } else if(p.getStatus() == 0&&nowDate.compareTo(p.getBegin()) < 0){
                        p.setStatus(2);
                    }else
                        return false;
                    return true;
                }).collect(Collectors.toList());
        return upList;
    }
     /**
     * 查询评议
     * 
     * @param id 评议主键
     * @return 评议
     */
    @Override
    public Review selectReviewById(Long id)
    {
        return reviewMapper.selectReviewById(id);
    }

    /**
     * 查询评议列表
     * 
     * @param review 评议
     * @return 评议
     */
    @Override
    public List<Review> selectReviewList(Review review)
    {
        return reviewMapper.selectReviewList(review);
    }

    /**
     * 新增评议
     * 
     * @param review 评议
     * @return 结果
     */
    @Override
    public int insertReview(Review review)
    {
        review.setCreateTime(DateUtils.getNowDate());
        return reviewMapper.insertReview(review);
    }

    /**
     * 修改评议
     * 
     * @param review 评议
     * @return 结果
     */
    @Override
    public int updateReview(Review review)
    {
        review.setUpdateTime(DateUtils.getNowDate());
        return reviewMapper.updateReview(review);
    }

    /**
     * 批量删除评议
     * 
     * @param ids 需要删除的评议主键
     * @return 结果
     */
    @Override
    public int deleteReviewByIds(Long[] ids)
    {
        return reviewMapper.deleteReviewByIds(ids);
    }

    /**
     * 删除评议信息
     * 
     * @param id 评议主键
     * @return 结果
     */
    @Override
    public int deleteReviewById(Long id)
    {
        return reviewMapper.deleteReviewById(id);
    }
}
